﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.Model;
using QuanLyHocPhi.BLL;

namespace QuanLyHocPhi.GUI.TacNghiep
{
    public partial class FormQuanLyLopHocPhanChuyenNganh : FormBase
    {
        #region Thuộc tính
        private LopHocPhan SelectedLopHocPhan
        {
            get
            {
                if (bsLopHocPhan.DataSource == null || bsLopHocPhan.Current == null)
                    return null;
                else
                    return bsLopHocPhan.Current as LopHocPhan;
            }
        }

        private string SelectedNamHoc
        {
            get
            {
                if (cbxNamHoc.SelectedItem == null)
                    return null;
                else
                    return cbxNamHoc.SelectedItem.ToString();
            }
        }
        #endregion

        /// <summary>
        /// Hàm khởi dựng
        /// </summary>
        /// <param name="hocKy"></param>
        public FormQuanLyLopHocPhanChuyenNganh()
        {
            InitializeComponent();
            m_LockUpdate = true;
            //HocKy = hocKy;
            // Nạp danh sách năm học
            NapDanhSachNamHoc(null);
            NapDanhSachHocKy(this.SelectedNamHoc, -1);
            NapDanhSachLopHocPhan(this.SelectedHocKy);
            //cmbTruong.DataSource = BizTruong.GetList_Truong();
            //cmbTruong.DisplayMember = "TenTruong";
            //cmbTruong.ValueMember = "MaTruong";
            HienThiThongTinChiTietLopHocPhan();
            m_LockUpdate = false;
            cmbTruong_SelectedIndexChanged(this, new EventArgs());
        }

        private void NapDanhSachNamHoc(string namHocToFocus)
        {
            var lstNamHoc = BizHocKy.GetList_NamHoc();
            cbxNamHoc.DataSource = lstNamHoc;
            if (namHocToFocus.IsNullOrEmpty() == false)
            {
                for (int i = 0; i < lstNamHoc.Count; i++)
                    if (lstNamHoc[i] == namHocToFocus)
                    {
                        cbxNamHoc.SelectedIndex = i;
                        break;
                    }
            }
        }

        private void NapDanhSachHocKy(string namHoc, int hocKyToFocus)
        {
            var lstHocKy = BizHocKy.GetList_HocKy(namHoc);
            cbxHocKy.DisplayMember = "SoHieuHocKy";
            cbxHocKy.DataSource = lstHocKy;
            if (hocKyToFocus > 0)
                for (int i = 0; i < lstHocKy.Count; i++)
                    if (lstHocKy[i].SoHieuHocKy == hocKyToFocus)
                    {
                        cbxHocKy.SelectedIndex = i;
                        break;
                    }
        }

        /// <summary>
        /// Nạp danh sách lớp học phần trong học kỳ đang tác nghiệp
        /// </summary>
        private void NapDanhSachLopHocPhan(HocKy hocKy)
        {
            if (hocKy == null)
            {
                panelLopHocPhan.Enabled = false;
                ucLopHocPhan_DanhSachSinhVien.LopHocPhan = null;
            }
            else
            {
                panelLopHocPhan.Enabled = true;
            }
            string maTruong = "GDTC";
            //if (cmbTruong.SelectedIndex != -1)
            //    maTruong = cmbTruong.SelectedValue.ToString();
            var lstLopHocPhan = BizLopHocPhan.GetList_LopHocPhan(hocKy, maTruong);
            if (lstLopHocPhan == null)
                return;
            lblThongKe.Text = (hocKy == null) ? "" : string.Format("Danh sách có {0} lớp học phần", lstLopHocPhan.Count);
            bsLopHocPhan.DataSource = lstLopHocPhan;
            gridLopHocPhan.AutoGenerateColumns = false;
            gridLopHocPhan.AllowUserToAddRows = false;
            gridLopHocPhan.AllowUserToDeleteRows = false;
            gridLopHocPhan.ReadOnly = true;
            colLopHocPhan_MaLop.DataPropertyName = "MaLopHocPhan";
            colLopHocPhan_TenLop.DataPropertyName = "TenLopHocPhan";
            colLopHocPhan_NgayBatDau.DataPropertyName = "NgayBatDau";
            colLopHocPhan_NgayKetThuc.DataPropertyName = "NgayKetThuc";
            gridLopHocPhan.DataSource = bsLopHocPhan;
            HienThiThongTinChiTietLopHocPhan();
        }

        #region Hiển thị thông tin chi tiết lớp học phần đang chọn
        private void OnBsLopHocPhanCurrentChanged(object sender, EventArgs e)
        {
            HienThiThongTinChiTietLopHocPhan();
        }

        private void HienThiThongTinChiTietLopHocPhan()
        {
            var selectedLopHocPhan = this.SelectedLopHocPhan;
            ucLopHocPhan_DanhSachSinhVien.Truong = BizTruong.GetTruongByMaTruong("GDTC");
            ucLopHocPhan_DanhSachSinhVien.LopHocPhan = selectedLopHocPhan;
            ucLopHocPhan_DanhSachSinhVien.IsChuyen = true;
            btnLopHocPhan_Edit.Enabled = btnLopHocPhan_Delete.Enabled = (selectedLopHocPhan != null);
        }
        #endregion

        #region Thêm, xoá, sửa Lớp học phần
        private void OnLopHocPhan_AddNewClick(object sender, EventArgs e)
        {
            LopHocPhan newLopHocPhan = new LopHocPhan
            {
                MaLopHocPhan = Guid.NewGuid().ToString(),
                MaHocKy = this.SelectedHocKy.MaHocKy,
                HocPhi = this.SelectedHocKy.HocPhiChuyen,
                PhuThu = -this.SelectedHocKy.PhuThuChuyen,
                TrangThaiHoatDong = LopHocPhan.TrangThai_ChuaThuXongHocPhi
            };
            FormLopHocPhan_Details frm = new FormLopHocPhan_Details(newLopHocPhan, isAdding: true);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                bsLopHocPhan.Add(newLopHocPhan);
                bsLopHocPhan.Position = bsLopHocPhan.Count - 1;
            }
        }

        private void OnLopHocPhan_EditClick(object sender, EventArgs e)
        {
            LopHocPhan lopHocPhan = this.SelectedLopHocPhan;
            if (lopHocPhan != null)
            {
                FormLopHocPhan_Details frm = new FormLopHocPhan_Details(lopHocPhan, isAdding: false);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    bsLopHocPhan.ResetCurrentItem();
                    HienThiThongTinChiTietLopHocPhan();
                }
            }
        }

        private void OnLopHocPhan_DeleteClick(object sender, EventArgs e)
        {
            LopHocPhan selectedLopHocPhan = this.SelectedLopHocPhan;
            if (selectedLopHocPhan != null)
            {
                try
                {
                    BizLopHocPhan.Delete_LopHocPhan(selectedLopHocPhan);
                    this.LockUpdate();
                    bsLopHocPhan.RemoveCurrent();
                    this.UnlockUpdate();
                    HienThiThongTinChiTietLopHocPhan();
                }
                catch (Exception exc)
                {
                    DialogUtils.ShowError(exc.Message);
                }
            }
        }

        private void OnGridLopHocPhanCellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.SelectedLopHocPhan != null)
                btnLopHocPhan_Edit.PerformClick();
        }
        #endregion

        private void OnLoad(object sender, EventArgs e)
        {

        }

        private int SelectedSoHieuHocKy
        {
            get
            {
                HocKy hocKy = null;
                if (cbxHocKy.SelectedItem != null)
                    hocKy = cbxHocKy.SelectedItem as HocKy;
                if (hocKy == null)
                    return -1;
                else
                    return hocKy.SoHieuHocKy;
            }
        }

        private HocKy SelectedHocKy
        {
            get
            {
                if (cbxHocKy.SelectedItem != null)
                    return cbxHocKy.SelectedItem as HocKy;
                else
                    return null;
            }
        }

        private void OnCbxNamHocSelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_LockUpdate)
                return;
            string namHoc = null;
            if (cbxNamHoc.SelectedItem != null)
                namHoc = cbxNamHoc.SelectedItem.ToString();
            NapDanhSachHocKy(namHoc, SelectedSoHieuHocKy);
        }

        private void OnCbxHocKySelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_LockUpdate)
                return;
            NapDanhSachLopHocPhan(this.SelectedHocKy);
        }

        private void cmbTruong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_LockUpdate)
                return;
            NapDanhSachLopHocPhan(this.SelectedHocKy);
        }


    }
}
